<?php

use Core\CashierController;
use Models\User;
use Models\UserLevel;
use Models\Money_log;
use Models\Cashier;
use Models\Address;
use Models\Recharge;

class UserController extends CashierController
{
	/**
	 * 通过手机号码获取会员信息
	 */
	public function find_user()
	{
		$phone = $this->input->get('phone');
		$user = User::select('id,level,avatar,nickname,first_name,last_name,balance,wechat')->where('phone',$phone)->get_one();
		if(!$user){
			return $this->error('404');
		}
		if($user['status'] == 1){
			return $this->error('400');
		}

		$user['discount'] = 1;
		$address = (new Address)->where('user_id', $user['id'])->get_one();
		if ($address) {
			$address = $address->region($address['id']);
		}
		$user_level = (new UserLevel)->find($user['level']);
		$user['address'] = $address ? $address['region'] : '';
		$user['zip'] = $address ? $address['zip'] : '';
		if($user_level) $user['discount'] = $user_level['discount'];
		$this->success($user->to_array());
	}

	/**
	 * 会员充值
	 */
	public function charge(){
		$user = (new User)->find($this->input->post('user_id'));
		if(!$user){
			return $this->error();
		}
		$money = $this->input->post('money');

		$add = Recharge::getPresent($money);

		$recharge = new Recharge();
		$recharge->load([
			'user_id' => $user['id'],
			'cashier_id' => $this->session->cashier['id'],
			'last' => $user['balance'],
			'now' => $user['balance'] + $recharge['money'],
			'money' => $money+ $add,
			'recharge' => $money,
			'present' => $add,
			'status' => 3,
			'pay_mode' => $this->input->post('pay_type'),
			'create_time' => date('Y-m-d H:i:s')
		]);
		$recharge->add();
		$user->levelUpIfNeed($recharge['money']);
		$user->update($user['id'], [
			'balance' => $user['balance'] + $recharge['money']
		]);
		$recharge->saveMoneylog([
			'type' => 3,
			'cashier_id' => $this->session->cashier['id'],
			'last' => $user['balance'],
			'now' => $user['balance'] + $recharge['money'],
			'desc' => "收银端充值 {$recharge['recharge']} 送 {$recharge['present']}",
			'pay_type' => $this->input->post('pay_type'),
			'from' => 'cashier'
		]);

		$log_data = $recharge->getMoneyLog(3);

		$log_data['user'] = $user;
		$log_data['cashier_name'] = $this->session->cashier['username'];

		return $this->success($log_data);
	}


	/**
	 * 解锁
	 * @return
	 */
	public function unlock(){
		$password = $this->input->post('password');
		$cashier = (new Cashier)->find($this->session->cashier['id']);
		if(encrypt($password) == $cashier['password']){
			return $this->success();
		}else{
			return $this->error();
		}
	}

	/**
	 * 注册会员
	 */
	public function reg(){
		$data['phone'] = $this->input->post('phone');
		$data['first_name'] = $this->input->post('first_name');
		$data['last_name'] = $this->input->post('last_name');
		$data['email'] = $this->input->post('email');
		$data['sex'] = $this->input->post('sex');
		$data['pay_pass'] = $this->input->post('pay_pass');
		$data['wechat'] = $this->input->post('wechat');
		$data['birthday'] = $this->input->post('birthday');
		$data['balance'] = $this->input->post('money');
		$data['credits'] = $data['total_credits'] = $this->input->post('credits');
		$data['cashier_id'] = $this->session->cashier['id'];
		$data['remark'] = $this->input->post('remark');

		if(!$data['phone'] || !$data['first_name'] || !$data['last_name']){
			return $this->error('1');
		}

		$c = User::where('phone',$data['phone'])->counts();
		if($c>0){
			return $this->error('2');
		}

		if($data['email']){
			$e = User::where('email',$data['email'])->counts();
			if($e>0){
				return $this->error('3');
			}
		}


		$data['nickname'] = $data['username'] = $data['first_name'].' '.$data['last_name'];
		$data['avatar'] = '/assets/avatar.png'; //默认头像
		$data['create_at'] = date('Y-m-d H:i:s');
		$user_level = UserLevel::where('credits <=',$data['balance'])->order_by('credits','desc')->get_one();
		if($user_level){
			$data['level'] = $user_level['id'];
		}

		//加入资金记录表
		$add = Recharge::getPresent($data['balance']);

		$data1 = $data;
		$data1['balance'] = $data['balance'] + $add;
		$user_id = (new User)->add($data1);
		if(!$user_id){
			return $this->error();
		}

		$recharge = new Recharge();
		$recharge->load([
			'user_id' => $user_id,
			'cashier_id' => $this->session->cashier['id'],
			'last' => 0,
			'now' => $data['balance'] + $add,
			'money' => $data['balance'] + $add,
			'recharge' => $data['balance'],
			'present' => $add,
			'status' => 3,
			'pay_mode' => $this->input->post('pay_type'),
			'create_time' => date('Y-m-d H:i:s')
		]);
		$recharge->add();

		$recharge->saveMoneylog([
			'type' => 3,
			'cashier_id' => $this->session->cashier['id'],
			'last' => 0,
			'now' => $data1['balance'],
			'desc' => "收银端注册充值 {$recharge['recharge']} 送 {$recharge['present']}",
			'pay_type' => $this->input->post('pay_type'),
			'from' => 'cashier'
		]);

		return $this->success();
	}
}
